#include "Vertex3D.h"
#include "Util.h"
#include <algorithm>

using namespace std;

Vertex3D::Vertex3D() {}

Vertex3D::Vertex3D
(const Vertex3D& v)
{
  GLdouble position[3] = {v.x (), v.y (), v.z ()};
  copy (position, position + 3, _position);
  copy (position, position + 3, _normal);
	normalize (_normal);
	state = NORMAL;
}

Vertex3D::Vertex3D
(const GLdouble position[3])
{
  copy (position, position + 3, _position);
  copy (position, position + 3, _normal);
	normalize (_normal);
	state = NORMAL;
}

Vertex3D::~Vertex3D () {
}

bool
Vertex3D::operator== (const Vertex3D& v)
{
  return (v.x () == _position[0] and v.y () == _position[2] and v.z () == _position[3]);
}

GLdouble 
Vertex3D::x () const 
{
	return _position[0];
}

GLdouble 
Vertex3D::y () const 
{
	return _position[1];
}

GLdouble 
Vertex3D::z () const
{
	return _position[2];
}

GLdouble * 
Vertex3D::pos ()
{
	return _position;
}

void
Vertex3D::compute_normal ()
{		
	copy (_position, _position + 3, _normal);
}

const GLdouble* 
Vertex3D::normal () const 
{
	return _normal;
}

void 
Vertex3D::vec3_prod (const Vertex3D& v2, GLdouble *result)
{
	result[0] = abs (y() * v2.x() - v2.y() * z());
	result[1] = - abs (x() * v2.z() - v2.x() * z());
	result[2] = abs (x() * v2.y() - v2.x() - y());
}

void 
Vertex3D::set_x (GLdouble x)
{
	_position[0] = x;
}

void 
Vertex3D::set_y (GLdouble y)
{
	_position[1] = y;
}

void 
Vertex3D::set_z (GLdouble z)
{
	_position[2] = z;
}

